using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using TVT.Common.Helpers;
using System.Net.Mail;
using System.Net;


namespace TVT.Common.EMailing
{
    public class EMailSender
    {
        public static void SendEmail(string sender, string to, string subject, string body)
        {
            SendEmail(sender, to, subject, body, false);
        }

        public static void SendEmail(string sender, string to, string subject, string body, bool isBodyHtml)
        {
            SendEmail(sender, to, subject, body, isBodyHtml, null);
        }
        
        public static void SendEmail(string sender, string to, string subject, string body, bool isBodyHtml, List<string> attachments)
        {
            SmtpClient smptClient = new SmtpClient();
            smptClient.Host = ConfigurationManager.AppSettings[TVT.Common.Helpers.WellKnownKeys.EmailHostKey].ToString();            

            string from = ConfigurationManager.AppSettings[TVT.Common.Helpers.WellKnownKeys.MailFromKey].ToString();
            MailAddress fromAddress = new MailAddress(from, sender);            
            MailMessage mail = new MailMessage();
            mail.From = fromAddress;
            mail.IsBodyHtml = isBodyHtml;            
            //smptClient.Credentials = new NetworkCredential(

            if (attachments != null)
            {                
                foreach (string item in attachments)
                {
                    mail.Attachments.Add(new Attachment(item));
                }
            }

            foreach (string mailTo in to.Split(';'))
            {
                try
                {
                    mail.To.Add(new MailAddress(mailTo));
                }
                catch (Exception)
                {
                                        
                }
            }            
                        
            mail.Body = body;
            mail.Subject = subject;
            
            smptClient.Send(mail);
        }
    }
}


